package can;

import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import robocode.control.snapshot.BulletState;
import robocode.control.snapshot.IBulletSnapshot;
import robocode.control.testing.RobotTestBed;
import robocode.control.events.BattleCompletedEvent;

/**
 * Illustrates JUnit testing of Robocode robots. This test simply verifies that ConnellBot always
 * beats Walls.
 * 
 * @author Andrea
 */
public class TestConnellBotAccuracy extends RobotTestBed {

  int hit = 0;
  int fired = 0;
  List<Integer> pastBullets = new ArrayList<Integer>();

  /**
   * Specifies that Walls and ConnellBot are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.Walls,can.ConnellBot";
  }

  /**
   * This test runs for 15 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 15;
  }

  /**
   * After each turn, look at the bullets on the field and keep track of how many hit and missed.
   * 
   * @param event The turn that just ended.
   */
  @Override
  public void onTurnEnded(robocode.control.events.TurnEndedEvent event) {
    IBulletSnapshot[] bullets = event.getTurnSnapshot().getBullets();
    for (IBulletSnapshot b : bullets) {
      if (b.getOwnerIndex() == 1 && !pastBullets.contains(b.getBulletId())) {
        if (b.getState() == BulletState.HIT_VICTIM) {
          pastBullets.add(b.getBulletId());
          hit++;
        }
        else if (b.getState() == BulletState.FIRED) {
          fired++;
        }
      }
    }
  }

  /**
   * The actual test, which asserts more bullets hit the target than missed.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    System.out.println(hit + " hit, " + fired + " fired");
    Assert.assertTrue(hit > fired / 2);
  }
}
